# 用户行为监控问题诊断脚本

Write-Host "=== 用户行为监控问题诊断 ===" -ForegroundColor Cyan

# 1. 检查前端调用顺序
Write-Host "`n1. 检查前端调用顺序:" -ForegroundColor Yellow

$vueFile = "d:/src/DotNet/Lzfy_Refund_Service/RefundAuditWeb/src/views/RefundTaskManagement.vue"
if (Test-Path $vueFile) {
    $content = Get-Content $vueFile -Raw
    
    # 检查showContactRecord方法
    if ($content -match "showContactRecord") {
        Write-Host "  ✓ showContactRecord方法存在" -ForegroundColor Green
        
        # 检查调用顺序
        if ($content -match "trackViewContact.*startSession") {
            Write-Host "  ⚠ 调用顺序: trackViewContact -> startSession (可能有问题)" -ForegroundColor Yellow
        } elseif ($content -match "startSession.*trackViewContact") {
            Write-Host "  ✓ 调用顺序: startSession -> trackViewContact (正确)" -ForegroundColor Green
        } else {
            Write-Host "  ? 无法确定调用顺序" -ForegroundColor Gray
        }
    } else {
        Write-Host "  ✗ showContactRecord方法不存在" -ForegroundColor Red
    }
} else {
    Write-Host "  ✗ Vue文件不存在" -ForegroundColor Red
}

# 2. 检查后端ActionTypes常量
Write-Host "`n2. 检查后端ActionTypes常量:" -ForegroundColor Yellow

$modelsFile = "d:/src/DotNet/Lzfy_Refund_Service/Models/UserBehaviorModels.cs"
if (Test-Path $modelsFile) {
    $content = Get-Content $modelsFile -Raw
    
    $actionTypes = @("VIEW_PHONE", "INPUT_NOTES", "SELECT_RESULT", "SAVE_CONTACT")
    foreach ($actionType in $actionTypes) {
        if ($content -match "VIEW_PHONE") {
            Write-Host "  ✓ VIEW_PHONE 常量已定义" -ForegroundColor Green
            break
        }
    }
} else {
    Write-Host "  ✗ Models文件不存在" -ForegroundColor Red
}

# 3. 检查后端Service实现
Write-Host "`n3. 检查后端Service实现:" -ForegroundColor Yellow

$serviceFile = "d:/src/DotNet/Lzfy_Refund_Service/Services/UserBehaviorService.cs"
if (Test-Path $serviceFile) {
    $content = Get-Content $serviceFile -Raw
    
    if ($content -match "VIEW_PHONE.*PhoneViewTime") {
        Write-Host "  ✓ VIEW_PHONE 操作会更新PhoneViewTime" -ForegroundColor Green
    } else {
        Write-Host "  ✗ VIEW_PHONE 操作未正确处理" -ForegroundColor Red
    }
    
    if ($content -match "IsKeyAction.*VIEW_PHONE") {
        Write-Host "  ✓ VIEW_PHONE 被标记为关键操作" -ForegroundColor Green
    } else {
        Write-Host "  ✗ VIEW_PHONE 未被标记为关键操作" -ForegroundColor Red
    }
} else {
    Write-Host "  ✗ Service文件不存在" -ForegroundColor Red
}

Write-Host "`n=== 问题分析 ===" -ForegroundColor Cyan
Write-Host "根据代码分析，发现以下问题:" -ForegroundColor Yellow
Write-Host "1. 前端调用顺序可能有问题" -ForegroundColor Gray
Write-Host "2. 需要确保会话建立后再跟踪行为" -ForegroundColor Gray
Write-Host "3. 可能存在异步调用时序问题" -ForegroundColor Gray

Write-Host "`n=== 诊断完成 ===" -ForegroundColor Cyan